package com.puboot.module.blog.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.puboot.common.util.CoreConst;
import com.puboot.common.util.Pagination;
import com.puboot.common.util.ResultUtil;
import com.puboot.exception.ArticleNotFoundException;
import com.puboot.module.admin.model.BizArticle;
import com.puboot.module.admin.model.BizArticleTags;
import com.puboot.module.admin.model.BizCategory;
import com.puboot.module.admin.model.BizTags;
import com.puboot.module.admin.service.BizArticleService;
import com.puboot.module.admin.service.BizArticleTagsService;
import com.puboot.module.admin.service.BizCategoryService;
import com.puboot.module.admin.service.BizTagsService;
import com.puboot.module.admin.vo.ArticleConditionVo;
import com.puboot.module.admin.vo.base.PageResultVo;
import com.puboot.module.admin.vo.base.ResponseVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.List;

import static com.puboot.common.util.CoreConst.THEME_PREFIX;

/**
 * 分类API控制
 * */
@RestController
@Slf4j
@AllArgsConstructor
@RequestMapping("app/api")
public class AppApiController {
    private final BizCategoryService categoryService;
    private final BizArticleService articleService;
    private final BizTagsService tagsService;

    private final BizArticleTagsService articleTagsService;

    /**
     * 获取分类列表 /app/api/categoryList?isFistLevel=true
     * */
    @GetMapping("/categoryList")
    public List<BizCategory> loadCategory(boolean isFistLevel) {
        BizCategory bizCategory = new BizCategory();
        bizCategory.setStatus(CoreConst.STATUS_VALID);
        if (isFistLevel) {
            bizCategory.setPid(CoreConst.TOP_MENU_ID);
        }
        List<BizCategory> catList = null;
        catList = categoryService.list();
        for (BizCategory category:catList) {
            ArticleConditionVo conditionVo = new ArticleConditionVo();
            QueryWrapper<BizArticle> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("category_id",category.getId());
            long count = articleService.count(queryWrapper);
            category.setCount((int)count);
        }
        return catList;
    }
    /**
     * 获取分类详细信息
     * */
    @GetMapping("/category/{id}")
    public BizCategory getCategoryId(@PathVariable("id") Integer id) {
        BizCategory bizCategory = categoryService.getById(id);
        QueryWrapper<BizArticle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",id);
        long count = articleService.count(queryWrapper);
        bizCategory.setCount((int)count);
        return bizCategory;
    }

    /**
     * 获取轮播文章
     * */
    @GetMapping("/sliderList")
    public ResponseVo sliderList() {
        List<BizArticle> sliderList =  articleService.sliderList();//轮播文章
        return ResultUtil.success("返回成功",sliderList);
    }

    /**
     * 获取置顶文章列表 categoryId pageNumber pageSize
     * pageNumber: 0
     * pageSize: 8
     * top: true
     * */
    @GetMapping("/articleTopList/{pageNumber}")
    public PageResultVo articleTopList(@PathVariable("pageNumber") Integer pageNumber) {
        ArticleConditionVo vo = new ArticleConditionVo();
        vo.setTop(true);
        vo.setPageNumber(pageNumber);
        IPage<BizArticle> page = new Pagination<>(vo.getPageNumber(), vo.getPageSize());
        List<BizArticle> articleList = articleService.findByCondition(page, vo);
        return ResultUtil.table(articleList, page.getTotal());
    }

    /**
     * 获取文章列表 categoryId pageNumber pageSize
     * pageNumber: 0
     * pageSize: 8
     * top: true categoryId: 1,
     * 					pageNumber: 1,
     * */
    @PostMapping("/articleList")
    public PageResultVo articleList(ArticleConditionVo vo) {
        IPage<BizArticle> page = new Pagination<>(vo.getPageNumber(), vo.getPageSize());
        List<BizArticle> articleList = articleService.findByCondition(page, vo);
        return ResultUtil.table(articleList, page.getTotal());
    }

    /**
     * 站长推荐
     * */
    @GetMapping("/recommendedList")
    public  List<BizArticle> recommendedList() {
        List<BizArticle> articleList = articleService.recommendedList(CoreConst.PAGE_SIZE);
        return articleList;
    }
    /**
     * 最近文章
     * */
    @GetMapping("/recentList")
    public  List<BizArticle> recentList() {
        List<BizArticle> articleList = articleService.recentList(CoreConst.PAGE_SIZE);
        return articleList;
    }
    /**
     * 随机文章
     * */
    @GetMapping("/randomList")
    public  List<BizArticle> randomList() {
        List<BizArticle> articleList = articleService.randomList(CoreConst.PAGE_SIZE);
        return articleList;
    }
    /**
     * 热门文章
     * */
    @GetMapping("/hotList")
    public  List<BizArticle> hotList() {
        List<BizArticle> articleList = articleService.hotList(CoreConst.PAGE_SIZE);
        return articleList;
    }

    /**
     * 文章详情
     * @param articleId
     * @return
     */
    @GetMapping("/article/{articleId}")
    public BizArticle article(@PathVariable("articleId") Integer articleId) {
        BizArticle article = articleService.selectById(articleId);
        if (article == null || CoreConst.STATUS_INVALID.equals(article.getStatus())) {
            throw new ArticleNotFoundException();
        }
       return article;
    }

    /**
     * 标签列表
     * @return
     */
    @PostMapping("tagslist")
    @ResponseBody
    public PageResultVo loadTags(BizTags bizTags, Integer pageNumber, Integer pageSize) {
        IPage<BizTags> bizTagsPage = tagsService.pageTags(bizTags, pageNumber, pageSize);
        List<BizTags> targsList = bizTagsPage.getRecords();
        for (BizTags targ:targsList) {
            QueryWrapper<BizArticleTags> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("tag_id",targ.getId());
            long count = articleTagsService.count(queryWrapper);
            targ.setCount((int)count);
        }
        return ResultUtil.table(targsList, bizTagsPage.getTotal());
    }


    /**
     * 获取标签内容
     * @return
     */
    @GetMapping("getTag/{id}")
    @ResponseBody
    public ResponseVo getTagsById(@PathVariable("id")Integer id) {
        BizTags  tags = tagsService.getById(id);
        if(tags != null) {
            QueryWrapper<BizArticleTags> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("tag_id", tags.getId());
            long count = articleTagsService.count(queryWrapper);
            tags.setCount((int) count);
            return ResultUtil.vo(CoreConst.SUCCESS_CODE, null, tags);
        }else
        {
            return ResultUtil.error("未发现标签");
        }
    }
}
